package com.ido.otaclient.font;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.ido.otaclient.R;
import com.ido.otaclient.StatusDownload;
import com.ido.otaclient.base.BaseActivity;
import com.ido.otaclient.base.IDLog;
import com.ido.otaclient.dialog.WaitingDialog;
import com.ido.otaclient.dialog.WhiteConfirmDialog;
import com.ido.otaclient.module.Veneer;
import com.ido.otaclient.util.ResourceUtil;
import com.ido.otaclient.util.ToastUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FontActivity extends BaseActivity implements FontContract.View{
    /**
     * 返回按钮
     */
    @BindView(R.id.title_leftBtn)
    ImageButton mTitleLeftBtn;
    /**
     * 标题内容
     */
    @BindView(R.id.title_text)
    TextView mTitleText;
    /**
     * 字库选择图片
     */
    @BindView(R.id.iv_choose_font)
    ImageButton mIvChooseFont;
    /**
     * 字库选择文件
     */
    @BindView(R.id.tv_choose_file_font)
    TextView mTvChooseFileFont;
    /**
     * 字库版本
     */
    @BindView(R.id.et_version_font)
    EditText mEtVersionFont;
    /**
     * 最新固件
     */
    @BindView(R.id.iv_choose_new)
    ImageButton mIvChooseNew;
    /**
     * 最新固件名称
     */
    @BindView(R.id.tv_choose_file_new)
    TextView mTvChooseFileNew;
    /**
     * 最新固件版本
     */
    @BindView(R.id.et_version_new)
    EditText mEtVersionNew;
    /**
     * 设备id
     */
    @BindView(R.id.et_device_id)
    EditText mEtDeviceId;
    /**
     * 确认配置
     */
    @BindView(R.id.btn_confirm)
    Button btnConfirm;

    /**
     * font文件路径
     */
    private String mFontPath;

    /**
     * 新固件路径
     */
    private String mNewPath;

    /**
     * 压缩的文件夹
     */
    private String mPathFolderZip;

    private static final String EXTRA_VENEER_LIST = "veneer_list";

    /**
     * 已连接的单板
     */
    private List<Veneer> mVeneerList = new ArrayList<>();

    private FontContract.Presenter mPresenter;

    /**
     * 是否全部下发完
     */
    private  boolean mIsComplete = false;

    /**
     * 选择字库文件
     */
    private static final int REQUEST_CODE_FONT = 1002;
    /**
     * 选择最新固件
     */
    private static final int REQUEST_CODE_NEW = 1003;

    public static void startActivity(Activity activity,List<Veneer> veneerList) {
        Intent intent = new Intent(activity, FontActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(EXTRA_VENEER_LIST, (Serializable) veneerList);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreateTask(Bundle savedInstanceState) {
        super.onCreateTask(savedInstanceState);
        setContentView(R.layout.activity_font);
        initTitle();
        initData();
    }

    private void initData() {
        Bundle bundle = getIntent().getExtras();
        if(bundle != null){
            mVeneerList = (List<Veneer>) bundle.getSerializable(EXTRA_VENEER_LIST);
            for(Veneer veneer : mVeneerList){
                IDLog.d(TAG, "initData: " + veneer.getIp());
                veneer.setDownloadStatus(StatusDownload.START.getCode());
            }
        }

        mPresenter = new FontPresenter(this);
        mPresenter.initConfig();
        showMessage(ResourceUtil.getString(R.string.upload_confirm_config));
    }

    private void initTitle() {
        mTitleText.setText("字库+新固升级");
    }

    @OnClick(R.id.title_leftBtn)
    public void back(View view) {
        ActivityCompat.finishAfterTransition(this);
    }


    @OnClick(R.id.iv_choose_font)
    public void toChooseFont(View view){
        mPresenter.chooseFile(REQUEST_CODE_FONT,this);
    }

    @OnClick(R.id.iv_choose_new)
    public void toChooseNew(View view){
        mPresenter.chooseFile(REQUEST_CODE_NEW,this);
    }

    @OnClick(R.id.btn_confirm)
    public void toConfirmConfig(View view){
        mPresenter.fileZip();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mPresenter.onActivityResult(FontActivity.this,requestCode,resultCode,data);
    }

    @Override
    public void showLoading() {
        WaitingDialog.showDialog(FontActivity.this);
    }

    @Override
    public void hideLoading() {
        WaitingDialog.hideDialog();
    }

    @Override
    public void showMessage(String message) {
        ToastUtils.show(message);
    }

    @Override
    public void setFontDrawable() {
        mIvChooseFont.setImageDrawable(ResourceUtil.getDrawable(R.drawable.ic_collection));
    }

    @Override
    public void showFontName(boolean isVisible) {
        mTvChooseFileFont.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    @Override
    public void setFontName(String fontName) {
        mTvChooseFileFont.setText(fontName);
    }

    @Override
    public String getFontName() {
        return mTvChooseFileFont.getText().toString();
    }

    @Override
    public void setFontPath(String fontPath) {
        mFontPath = fontPath;
    }

    @Override
    public String getFontPath() {
        return mFontPath;
    }

    @Override
    public void setFontVersion(String fontVersion) {
        mEtVersionFont.setText(fontVersion);
    }

    @Override
    public String getFontVersion() {
        return mEtVersionFont.getText().toString();
    }

    @Override
    public void setNewDrawable() {
        mIvChooseFont.setImageDrawable(ResourceUtil.getDrawable(R.drawable.ic_collection));
    }

    @Override
    public void showNewName(boolean isVisible) {
        mTvChooseFileNew.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    @Override
    public String getNewName() {
        return mTvChooseFileNew.getText().toString();
    }

    @Override
    public void setNewName(String newName) {
        mTvChooseFileNew.setText(newName);
    }

    @Override
    public String getNewPath() {
        return mNewPath;
    }

    @Override
    public void setNewPath(String newPath) {
        mNewPath = newPath;
    }

    @Override
    public String getNewVersion() {
        return mEtVersionNew.getText().toString();
    }

    @Override
    public void setNewVersion(String newVersion) {
        mEtVersionNew.setText(newVersion);
    }

    @Override
    public String getDeviceId() {
        return mEtDeviceId.getText().toString();
    }

    @Override
    public void setDeviceId(String deviceId) {
        mEtDeviceId.setText(deviceId);
    }

    @Override
    public void setPathFolderZip(String pathFolderZip) {
        mPathFolderZip = pathFolderZip;
    }

    @Override
    public String getPathFolderZip() {
        return mPathFolderZip;
    }

    @Override
    public List<Veneer> getVeneerList() {
        return mVeneerList;
    }

    @Override
    public void setVeneerList(List<Veneer> veneerList) {
        if(mVeneerList != null)
            mVeneerList.clear();
        mVeneerList.addAll(veneerList);
    }

    @Override
    public void setIsComplete(boolean isComplete) {
        mIsComplete = isComplete;
    }

    @Override
    public boolean getIsComplete() {
        return mIsComplete;
    }

    @Override
    public void back() {
        ActivityCompat.finishAfterTransition(this);
    }

    @Override
    public void showConfirmCancel() {
        WhiteConfirmDialog mWhiteConfirmDialog = WhiteConfirmDialog.newInstance(
                ResourceUtil.getString(R.string.upload_file_fail),
                ResourceUtil.getString(R.string.confirm),
                ResourceUtil.getString(R.string.cancel),true);
        mWhiteConfirmDialog.setOnConfirmListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //重新下发固件
                mPresenter.uploadAndConfirmFile();
                mWhiteConfirmDialog.dismissAllowingStateLoss();
            }
        });
        mWhiteConfirmDialog.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mIsComplete = false;
                //取消弹框
                mWhiteConfirmDialog.dismissAllowingStateLoss();
                hideLoading();
            }
        });

        mWhiteConfirmDialog.setCancelable(false);
        mWhiteConfirmDialog.show(getSupportFragmentManager());
    }

    @Override
    public void setPresenter(@NonNull FontContract.Presenter presenter) {
        mPresenter = presenter;
    }
}
